public class CreatePassword{
    public static String createPassword(int passwordLength){  //生成强密码
        if(passwordLength <8 || passwordLength >16){  //审查
            return "请设定的密码长度为8-16";
        }
        else{
            String password = "";
            boolean control = true;  //false意味着生成强密码成功退出循环
            while(control){
                for(int i = 0;i < passwordLength;i++){
                    int switchNum = (int)(Math.random()*4); //先分成大写字母、数字、小写字母,如果有需要,可给他们加权
                    char temp;  //每次生成一个字符
                    if(switchNum == 0){
                        temp =(char)('0' + (int)(Math.random()*10));
    
                    }
                    else if(switchNum == 1){
                        temp =(char)('a' + (int)(Math.random()*26));

                    }
                    else {
                        temp =(char)('A' + (int)(Math.random()*26));
                    }
                    password = password + temp;//一次次连接最终形成密码
                }
                if(password.matches(".*[A-Z].*") 
                    && password.matches(".*[0-9].*") 
                    && password.matches(".*[a-z].*")){  //审查,跟审查强密码的效果相同
                    control = false;
                }
            }
            return "生成的强密码为:" + password;  //返回
        }    
    }

}

